package com.jeecg.p3.base.util;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import org.jeecgframework.p3.core.common.utils.AjaxJson;
import org.jeecgframework.p3.core.utils.common.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

public class WxUtils {
    public static final Logger logger = LoggerFactory.getLogger(WxUtils.class);

    /**
     * 获取微信用户用户openid
     * @param request
     */
    public static AjaxJson getWxUserOpenId(HttpServletRequest request) {
        AjaxJson j = new AjaxJson();
        String code = request.getParameter("code");
        logger.info("code： " + code);
        if(StringUtils.isNotEmpty(code)) {
            String url = ComCode.getCodeUrl + "appid=" + ComCode.appid + "&secret=" + ComCode.secret + "&code=" + code
                    +"&grant_type=authorization_code";
            String tokenString = HttpUtil.get(url);
            logger.info("tokenString: " + tokenString);
            if(StringUtils.isNotEmpty(tokenString)) {
                JSONObject json = new JSONObject(tokenString);
                String access_token = json.getStr("access_token");
                String openid = json.getStr("openid");
                logger.info("access_token--->" + access_token + ",  openid-----> " + openid);
                if(StringUtils.isNotEmpty(openid)) {
                    j.setObj(openid);
                    return j;
                }
            }
        }
        j.setSuccess(false);
        j.setMsg("获取微信授权失败！");
        return j;
    }

    /**
     * 获取微信用户详细信息
     * @param request
     * @return
     */
    public static AjaxJson getWxUserDetail(HttpServletRequest request) {
        AjaxJson j = new AjaxJson();
        String code = request.getParameter("code");
        logger.info("code   ---->" + code);
        if(StringUtils.isNotEmpty(code)) {
            String url = ComCode.getCodeUrl + "appid=" + ComCode.appid + "&secret=" + ComCode.secret + "&code=" + code
                    + "&grant_type=authorization_code";
            String tokenString = HttpUtil.get(url);
            logger.info("tokenString  --->  " + tokenString);
            if (StringUtils.isNotEmpty(tokenString)) {
                JSONObject json = new JSONObject(tokenString);
                String access_token = json.getStr("access_token");
                String openid = json.getStr("openid");
                logger.info("access_token--->" + access_token + ",  openid-----> " + openid);

                if (StringUtils.isNotEmpty(access_token) && StringUtils.isNotEmpty(openid)) {
                    String userUrl = ComCode.getUserInfo + "access_token=" + access_token
                            + "&openid=" + openid + "&lang=zh_CN";
                    String userInfo = HttpUtil.get(userUrl);
                    if (StringUtils.isNotEmpty(userInfo)) {
                        JSONObject userJson = new JSONObject(userInfo);
                        WxUserInfo wxUserInfo = new WxUserInfo(openid,userJson.getStr("nickname"), userJson.getStr("sex"),
                                userJson.getStr("province"), userJson.getStr("city"), userJson.getStr("country"),
                                userJson.getStr("headimgurl"), userJson.getStr("privilege"), userJson.getStr("unionid"));
                        logger.info("wxUserInfo---------->" + wxUserInfo.toString());
                        j.setObj(wxUserInfo);
                        return j;
                    }
                }
            }
        }
        j.setSuccess(false);
        j.setMsg("获取微信授权失败！");
        return j;
    }
}
